/*
 import {
 user as UserApi
 } from '../../config/request.js';
 */

import * as types from '@/store/user/types';

const apiUrl = {
    login: '/api/login',
};

export default {
    name: 'login',
    data() {
        return {
            remumber: false,
            loading: false,
            data: {
                account: 'admin',
                password: 'adminasd',
            },
            rule_data: {
                account: [
                    {
                        required: true,
                        message: '用户名不能为空！',
                        trigger: 'blur',
                    }],
                password: [
                    {
                        required: true,
                        message: '密码不能为空！',
                        trigger: 'blur',
                    }],
            },
        };
    },
    methods: {
        login() {
            this.$refs.formLogin.validate((valid) => {
                if (valid) {
                    this.loading = true;

                    // 如果记住密码，提交的信息包括真实token，密码则是假的
                    // 服务端登录验证优先级：用户名必须，其次先取token，不存在时再取密码
                    this.$api.post(apiUrl.login, this.data).then((res) => {
                        this.loading = false;

                        if (res.data.success) {
                            this.$message({
                                message: '登录成功',
                                type: 'success',
                            });

                            this.$store.commit(types.USER_INFO, res.data.data);
                            this.$router.push('/');
                        } else {
                            this.$message({
                                message: res.data.msg,
                                type: 'warning',
                            });
                        }
                    }).catch(() => {
                        this.loading = false;
                    });
                }
            });
        },
        resetForm() {
            this.$refs.formLogin.resetFields();
        },
    },
};
